const cheerio = require('cheerio');
const request = require('superagent');
const slice = require('../utils/helpers').slice;
const { replace, map, zipObj, pipe } = require('ramda');

const getLessonsUrl = async (seriesPageUrl) => {
    const res = await request.get(seriesPageUrl);
    const $ = cheerio.load(res.text);
    const lessonsList = slice.call($('.table-responsive .title a'));

    const lessonInfoTransformer = (lesson) => {
        const formatLessonTitle = pipe(replace(/ /g, '-'), replace(/'s/g, ''));
        const infoArr = [
            formatLessonTitle($(lesson).text()),
            $(lesson).attr('href'),
        ];

        return zipObj(['title', 'url'], infoArr);
    };

    const getLessonsInfo = map(lessonInfoTransformer);

    return getLessonsInfo(lessonsList);
};

// const seriesPageUrl = 'https://egghead.io/playlists/learn-ramda-js-ec318ad7';
//
// (async () => {
//     await getLessonsUrl(seriesPageUrl);
// })();

module.exports = getLessonsUrl
;
